package com.jiangjiang.eshop.cart.controller;

import com.jiangjiang.eshop.cart.biz.ShoppingCartBiz;
import com.jiangjiang.eshop.cart.dto.AddShoppingCartItemDTO;
import com.jiangjiang.eshop.cart.dto.ShoppingCartDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ShoppingCartController
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/1 23:01
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartBiz shoppingCartBiz;

    /**
     * 向购物车中添加商品
     *
     * @param dto
     * @return
     */
    @PostMapping("/item/add")
    public Boolean addShoppingCartItem(@RequestBody AddShoppingCartItemDTO dto) {
        try {
            return shoppingCartBiz.addShoppingCartItem(dto.getUserAccountId(), dto.getGoodsSkuId());
        } catch (Exception e) {
            log.warn("ShoppingCartController addShoppingCartItem error", e);
        }
        return false;
    }

    /**
     * 查看购物车
     *
     * @param userAccountId 用户账户id
     * @return 查询结果
     */
    @GetMapping("/view/{userAccountId}")
    public ShoppingCartDTO viewShoppingCart(@PathVariable("userAccountId") Long userAccountId) {
        try {
            return shoppingCartBiz.viewShoppingCart(userAccountId);
        } catch (Exception e) {
            log.warn("ShoppingCartController viewShoppingCart error", e);
        }
        return null;
    }
}
